//
//  MyTravelViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/20.
//  Copyright © 2017年 Lwx. All rights reserved.
//
#define leftViewX 38

#import "MyTravelViewController.h"
#import "MyTravelTableViewCell.h"
#import "MyTravelDraftTableViewCell.h"
#import "NewTravelNoteViewController.h"
#import "MyTravelModel.h"
#import "DraftEditViewController.h"
#import "TravelsViewController.h"

@interface MyTravelViewController ()<UITableViewDelegate, UITableViewDataSource, MyTravelTableViewCellDelegate, MyTravelDraftTableViewCellDelegate>
@property (nonatomic, strong)UIButton * plusBtn;
@property (nonatomic, strong)UIView * topView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * leftView;
@property (nonatomic, strong)MyTravelModel * model;
@property (nonatomic, assign)int nowPage;
@property (nonatomic, assign)int didInt;//游记id、或者美拍id
@property (nonatomic, strong)NSMutableArray *dataArr;
@end

@implementation MyTravelViewController(Networking)
- (void)getMyTravelDataWithStateIfPublish:(BOOL)isPublish{
    NSDictionary * dic = @{
                           @"state":isPublish?@(1):@(2),
                           @"page":@(self.nowPage),
                           @"row":@(6),
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token
                           };
    WS(ws)
//    [self showprogressHUD];
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/diary/myDiary") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
//        [self hiddenProgressHUD];
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                if (ws.nowPage == 1)
                {
                    [ws.dataArr removeAllObjects];
                }
                ws.model = [MyTravelModel yy_modelWithDictionary:obj];
                [ws.dataArr addObjectsFromArray:ws.model.data];
                [ws.tableView reloadData];
            }
            else
            {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}

- (void)transformLikesDataWithDid:(int)did{
    NSDictionary * dic = @{
                           @"mid":[UserModel intance].member_id,
                           @"token":[UserModel intance].token,
                           @"did":@(did),
                           @"type":@(1)
                           };
    [[HttpRequest shardWebUtil] postNetworkRequestURLString:Url(@"/index/collect") andParas:dic andTransferGottenData:^(id obj, NSError *error) {
        if (error)
        {
            [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
        }
        else
        {
            if ([obj[@"code"] integerValue] == 100000)
            {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
                [self getMyTravelDataWithStateIfPublish:self.isPublish];
            }
            else
            {
                [[HUDHelper sharedInstance]tipMessage:obj[@"msg"]];
            }
        }
    }];
}

@end
@implementation MyTravelViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.isPublish) {
        self.leftView.x = leftViewX;
    }else{
        self.leftView.x = leftViewX + SCREEN_WIDTH / 2;
    }
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    _isPublish = YES;
    self.view.backgroundColor = getColor(bgColor);
    self.title = @"我的游记";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.plusBtn];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.tableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.dataArr = [NSMutableArray array];
}

- (UIButton *)plusBtn{
    if (!_plusBtn) {
        _plusBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 42 / 2, 40 / 2)];
        [_plusBtn setBackgroundImage:[UIImage imageNamed:@"xcyj_xinjian"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(addTravelNotePage) forControlEvents:UIControlEventTouchUpInside];
    }return _plusBtn;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100 / 2)];
        _topView.backgroundColor = [UIColor whiteColor];
        
        //右按钮
        UIButton * publishBtn = [[UIButton alloc] init];
        [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
        publishBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [publishBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        [publishBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        publishBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, _topView.height);
        [_topView addSubview:publishBtn];
        
        
        int lineViewW = SCREEN_WIDTH / 2 - 2 * leftViewX;
        int lineViewH = 3;
        int lineViewY = _topView.bottom - 3;
        self.leftView = [[UIView alloc] init];
        self.leftView.backgroundColor = getColor(@"ffbb02");
        self.leftView.frame = CGRectMake(leftViewX, lineViewY, lineViewW, lineViewH);
        [self.topView addSubview:self.leftView];
        
        //左按钮
        UIButton * draftBtn = [[UIButton alloc] init];
        [draftBtn setTitle:@"草稿" forState:UIControlStateNormal];
        draftBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [draftBtn addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];
        [draftBtn setTitleColor:getColor(@"6d6d6d") forState:UIControlStateNormal];
        draftBtn.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, _topView.height);
        [_topView addSubview:draftBtn];
        
        //中线
        int middleViewY = 9;
        int middleViewH = _topView.height - 2 * middleViewY;
        UIView * middleView = [[UIView alloc] init];
        middleView.backgroundColor = getColor(@"d2d2d2");
        middleView.frame = CGRectMake(SCREEN_WIDTH / 2, middleViewY, 1, middleViewH);
        [_topView addSubview:middleView];
        
    }return _topView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.height, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - self.topView.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = getColor(@"e5e5e5");
        UIView *clearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 10)];
        clearView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = clearView;
        WS(ws)
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            // 进入刷新状态后会自动调用这个block
            ws.nowPage++;
            [ws getMyTravelDataWithStateIfPublish:self.isPublish];
        }];

        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            ws.nowPage = 1;
            [ws getMyTravelDataWithStateIfPublish:self.isPublish];
        }] ;
        [self.tableView.mj_header beginRefreshing];
    }return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.model.data.count;
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_isPublish == YES) {
        NSString * cellID = @"publish";
        [self.tableView registerClass:[MyTravelTableViewCell class] forCellReuseIdentifier:cellID];
        MyTravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        if (!cell) {
            cell = [[MyTravelTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.modelData = self.dataArr[indexPath.row];
        cell.delegate = self;
        return cell;
    }else if (_isPublish == NO){
        NSString * cellID = @"draft";
        [self.tableView registerClass:[MyTravelDraftTableViewCell class] forCellReuseIdentifier:cellID];
        MyTravelDraftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        if (!cell) {
            cell = [[MyTravelDraftTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.modelData = self.dataArr[indexPath.row];
        cell.delegate = self;
        return cell;
    }else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220 / WIDTH_5S_SCALE;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TravelsViewController * travelsVC = [[TravelsViewController alloc] init];
    MyTravelTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    travelsVC.dID = [cell.modelData.signId intValue];
    NSLog(@"发现活动ID%i",travelsVC.dID);
    [self.navigationController pushViewController:travelsVC animated:YES];
}

#pragma mark - action
- (void)addTravelNotePage{
    NewTravelNoteViewController * newTravelNoteVC = [[NewTravelNoteViewController alloc] init];
    newTravelNoteVC.navigationItem.title = @"新建游记";
    [self.navigationController pushViewController:newTravelNoteVC animated:YES];
}

- (void)newTravelNotePage:(UIButton *)btn{
    DraftEditViewController * draftEditVC = [[DraftEditViewController alloc] init];
    draftEditVC.navigationItem.title = @"修改草稿";
    
    MyTravelDraftTableViewCell * cell = (MyTravelDraftTableViewCell *)[[btn superview] superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    MyTravelModelData * modelData = self.dataArr[indexPath.row];
    draftEditVC.travelID = modelData.signId;
    [self.navigationController pushViewController:draftEditVC animated:YES];
}

- (void)getLikesClickWithDid:(int)did{
    NSLog(@"喜欢/不喜欢");
    self.didInt = did;
    [self transformLikesDataWithDid:self.didInt];
}

- (void)clickMove:(UIButton *)sender
{
    if (sender.frame.origin.x + 1 > SCREEN_WIDTH / 2) {
        _isPublish = NO;
    }else{
        _isPublish = YES;
    }
     [self.tableView.mj_header beginRefreshing];
    WS(ws)
    [UIView animateWithDuration:0.3 animations:^{
        ws.leftView.x = sender.x + 38;
    }];
    
}

@end
